package com.example.base.tools.config;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.preference.PreferenceManager;
import android.util.Log;

import com.example.base.tools.tools.Acache;
import com.example.base.tools.tools.AppUtils;
import com.example.base.tools.tools.PackageUtils;

public class AppProfile {

    private static final boolean DEBUG = true;

    private static final String TAG = "AppProfile";
    private static Context mContext;

    private static AppProfile appProfile;
    public static final String Preferences_Boolean_Current_Verison_Is_Init = "AppProfile.Preferences_Boolean_Current_Verison_Is_Init";

    public static final String Preferences_Int_Current_Verison_Code = "Preferences_Int_Current_Verison_Code";
    private static final String ADDRESSVERSION = "addressVersion";

    private String addressVersion;

    private static Acache aCache;

    public static boolean isAsyn = true;// 是否需要后台同步账户信息

    public static boolean isLoginPage = false;// 是否停留在登录页

    private static SharedPreferences mPreferences;
    public static final String Preferences_Boolean_IsFirst_Guide = "com.zd.milibenben.Preferences_Boolean_IsFirst_Guide";//第一次打开，显示引导页

    public AppProfile(Context context) {
        mContext = context;
        if (mPreferences == null) {
            mPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
        }
        if (aCache == null) {
//            aCache = Acache.get(context);
        }
    }
    public static AppProfile getInstance(Context context) {
        if (DEBUG) Log.v(TAG, "getProfile(context)");
        if (appProfile == null) {
            appProfile = new AppProfile(context);
        }
        return appProfile;
    }

    public SharedPreferences getCoreConfig() {
        return mPreferences;
    }
    /**
     * 初始化程序.
     */
    public void init() {
        Log.v(TAG, "init()");

        int newVersion = PackageUtils.getLocalVersionCode(mContext);

        boolean needClearData = getBooleanMetaData("NeedClearData");

        if (needClearData && mContext != null) {

            int oldVersion = mPreferences.getInt(Preferences_Int_Current_Verison_Code, -1);

            GBPackageInfoCompat IMPL = new GBPackageInfoCompat(mContext);

            if ((oldVersion == -1 && IMPL.isUpgrade()) || newVersion > oldVersion) {

                onUpgrade(mContext, oldVersion, newVersion);

            }
        }
        mPreferences.edit().putInt(Preferences_Int_Current_Verison_Code, newVersion).commit();
    }
    /**
     * 判断是更新还是最初安装
     */
    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    public static class GBPackageInfoCompat {
        public GBPackageInfoCompat(Context context) {
        }

        public boolean isUpgrade() {
            if (Build.VERSION.SDK_INT >= 9) {
                PackageManager packageManager = mContext.getPackageManager();
                try {
                    PackageInfo packageInfo = packageManager.getPackageInfo(mContext.getPackageName(), 0);
                    long firstInstallTime = packageInfo.firstInstallTime;
                    long lastUpdateTime = packageInfo.lastUpdateTime;
                    if (lastUpdateTime > firstInstallTime) {
                        return true;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                return false;
            }

            return false;
        }
    }
    /**
     * 清理之前版本的存留数据,任何与当前版本不兼容的数据都应在次方中中清除或者升级.
     *
     * @param context
     */
    public void onUpgrade(Context context, int oldVersion, int newVersion) {
        Log.v(TAG, "onUpgrade(context, oldVersion=" + oldVersion + ", newVersion=" + newVersion + ")");
        try {
            // 清理之前版本的数据.
//            DataCleanUtils.cleanFiles(context);
//            DataCleanUtils.cleanDatabases(context);
//            DataCleanUtils.cleanInternalCache(context);
//            DataCleanUtils.cleanSharedPreference(context);
        } catch (Exception e) {
            // 清除数据,不希望有错!
        }
    }

    public boolean getBooleanMetaData(String key) {
        try {
            ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(mContext.getPackageName(),
                    PackageManager.GET_META_DATA);
            return ai.metaData.getBoolean(key, false);
        } catch (Exception e) {

        }
        return false;
    }


    /**
     * 设置是否是第一次打开，显示引导页
     */

    public void setGuideFirst(boolean isGuideFirst) {
        if (DEBUG) Log.v(TAG, "setGuideFirst()");
        mPreferences.edit().putBoolean(Preferences_Boolean_IsFirst_Guide, isGuideFirst).commit();
    }
    public boolean isGuideFirst() {
        if (DEBUG) Log.v(TAG, "isGuideFirst()");
        return mPreferences.getBoolean(Preferences_Boolean_IsFirst_Guide, true);
    }
    //false为第一次
    public boolean getHomeMask(){
        int homeMask = mPreferences.getInt("homeMask", -1);
        int versionCode = AppUtils.getVersionCode(mContext);
        if (homeMask==-1){
            return false;
        } else if (versionCode>homeMask){
            return false;
        }else {
            return true;
        }
    }


}
